﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _0912092_BaiTap01_SymbolsReport
{
    public class DemTanSoNhom3KyTu : DemTanSoNhomKyTu
    {
        protected override void KhoiTaoDanhSachNhomKyTu()
        {
            danhSachNhomKyTu = new List<NhomKyTu>();
            string[] dsWords = textInput.Split(new char[] { ' ' });
            int nWord = dsWords.Length;
            for (int i = 0; i < nWord; i++)
            {
                //Lay mot nhom 3 ky tu trong word.
                String curWord = dsWords[i];
                int lengthWord = curWord.Length;
                if (lengthWord >= 3)
                {
                    for (int j = 0; j < lengthWord - 2; j++)
                    {
                        String subStr = curWord.Substring(j, 3);
                        int nNhomKyTu = danhSachNhomKyTu.Count;
                        bool add = true;
                        for (int h = 0; h < nNhomKyTu; h++)
                        {
                            if (danhSachNhomKyTu[h].ChuoiKyTu.CompareTo(subStr) == 0)
                            {
                                add = false;
                                break;
                            }
                        }
                        if (add)
                        {
                            NhomKyTu nkt = new NhomKyTu();
                            nkt.ChuoiKyTu = subStr;
                            danhSachNhomKyTu.Add(nkt);
                        }
                    }
                }

            }
        }

        protected override void DemTanSoXuatHienCuaNhomKyTu()
        {
            int nNhomKyTu = danhSachNhomKyTu.Count;
            int lengthInput = textInput.Length;
            for (int i = 0; i < nNhomKyTu; i++)
            {
                String curNKT = danhSachNhomKyTu[i].ChuoiKyTu;
                int oldIndex = 0;
                int beginIndex = 0;
                while (beginIndex < lengthInput)
                {
                    int index = textInput.IndexOf(curNKT, beginIndex);
                    if (index >= 0)
                    {
                        beginIndex = index + 1; ;
                        danhSachNhomKyTu[i].TanSo++;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        protected override void HienThiTitle()
        {
            Console.WriteLine("*** TOP 10 NHOM 3 KY TU ***");
        }
    }
}
